Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
The declaration must be placed in one of two locations: • For calling the function only from within one form module, place the declaration in the General Declarations section of the module’s code. Remember to place the Private keyword at the beginning of the declaration, because Public declarations (the default) are not permitted in form modules. • When calling the function from more than one module, place it in a Basic module that is part of the project. Now let’s look at the function’s arguments. The first one, lpstrCommand, is a string containing the command that you want to send to the MCI (these are the commands shown previously). The second argument, lpstrReturnString, is used by the MCI to return a message to the calling program. Not all commands result in a return message. The third argument, uReturnLength, specifies the length of the returned string. The final argument is used to specify a callback function that is executed when the command has completed executing. The function returns zero if it is successful in carrying out the requested action, and a non-zero error code otherwise. Callback functions are used by some Windows API functions when they need to interact with the calling program. When a program calls an API function that uses a callback, it passes the address of the callback function as an argument to the API function. Callback itself is a regular Visual Basic function that you write, limited only by the requirements of the API function. To get the address of a function, use the AddressOf operator. Thus, if your callback function for use with mciSendString is named MyCallBack, you would call the API function as follows:
    retval = mciSendString(cmd, reply, 0, AddressOf(MyCallBack)
The multimedia tasks in this chapter are relatively simple, however, and do not require the use of a callback function. Later, I’ll show you another way to detect when a command has completed. Now for a real example. Once you have declared the function, you play a MIDI file as follows:
    cmd = “Open ” & Chr$(34) & “C:\win95\media\canyon.mid” & Chr$(34) & _
        “type sequencer alias canyon”
    retval = mciSendString(cmd, reply, 0, 0)
    cmd = “play canyon”
    retval = mciSendString(cmd, reply, 0, 0)
    ‘ In a real program we would have to wait
    ‘ for play to finish before closing the device.
    cmd = “close canyon”
    retval = mciSendString(cmd, reply, 0, 0)
Note how the file name itself is enclosed in double quotes by using Chr$(34). If you executed this code as is—with the Close command sent immediately after the Play command—you would hear nothing. The device would be closed immediately. You need some way to pause until the playback is complete—whether sound or video. Two methods are available for doing this. One method uses the Wait parameter to the Play command. Wait instructs the MCI not to accept any other commands, such as Close, until the current playback is complete:
    cmd = “play canyon wait”
    retval = mciSendString(cmd, reply, 0, 0)
    cmd = “close canyon”
    retval = mciSendString(cmd, reply, 0, 0)
With this code, the full CANYON.MID file will play. Because the program is frozen during playback, however, the user cannot continue with other tasks. Thus, Wait is a good solution for short sounds where a “frozen program” is not a problem. For longer playback, it clearly will not suffice. For this, you can use the Status command, which has the following syntax:
    Status device parameter
The device argument specifies which device’s status we are requesting; this is the name we assigned with the Alias keyword when we opened the device. The parameter argument specifies the actual status we are requesting. Table 16.3 lists the Status parameters commonly needed for sound and video devices. Here is a method that uses the Status command to delay issuing the Close command until the music file is finished playing:
    cmd = “Open ” & Chr$(34) & “C:\win95\media\canyon.mid” & Chr$(34) & _
        “type sequencer alias canyon”
    retval = mciSendString(cmd, reply, 0, 0)
    cmd = “play canyon”
    retval = mciSendString(cmd, reply, 0, 0)

    ‘ Wait for play to finish before closing.
    cmd = “status canyon mode”
    do
        retval = mciSendString(cmd, reply, 0, 0)
    until reply <> “playing”

    cmd = “close canyon”
    retval = mciSendString(cmd, reply, 0, 0)
Did you notice a problem with this approach? The program is not technically frozen as the sound plays, but it is effectively frozen while it mindlessly executes a loop over and over. We’ll see a way to use Visual Basic’s Timer control to get around this problem later in the demonstration program. We’ll also see how to use a callback function. First, let’s look at some of the other MCI commands. #### The Seek Command The Seek command repositions the pointer in a device. The pointer indicates the current playing position. Play stops when we use Seek, and any subsequent Play command resumes playing from the new position. The syntax of this command is:
    Seek to position
The most common use for Seek is to reset the file pointer to the beginning of the file, which is accomplished by using a value of zero for the position argument. If you want to seek to any location other than the beginning, you must specify the position argument in the device’s current TimeFormat (discussed later in the chapter). Table 16.3 Status parameters for sound and video devices.
Parameter | Meaning
--- | ---
length | Length of the file in frames (video) or samples (sound)
mode | Returns the device’s current mode: “not ready”, “paused”, “playing”, “seeking”, or “stopped”
position | Current position in the file; sample for sound, frame for video
ready | Returns True if the device is ready
window handle | Returns the handle of the window being used for video playback
#### The Set Command Set allows you to control certain